﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;

using ButtonBuilder.Plugin;

namespace SpeechPlugin
{
    [Description("Run Application")]
    public class RunAppAction : ButtonBuilder.Plugin.Action
    {
        public RunAppAction()
            : base()
        {
            Name = "Run Application";
        }

        public override void Init()
        {
            base.Init();

            if (GetParam("AppPath") == null)
            {
                PutFileParam("AppPath", string.Empty);
            }

            if (GetParam("Arguments") == null)
            {
                PutStringParam("Arguments", string.Empty);
            }
        }

        public override ActionResult DoIt()
        {
            ActionResult result = new ActionResult() { Source = this };

            if (string.IsNullOrEmpty(GetParam("AppPath")))
            {
                //Nothing to do.
                return result;
            }

            try
            {
                Process.Start(GetParam("AppPath"), GetParam("Arguments"));
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }

            return result;
        }
    }
}
